#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020/5/28 09:53
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 394. 字符串解码.2020-05-28.py
# @Software: PyCharm

__author__ = 'yangdy'


class Solution:
    def decodeString(self, s: str) -> str:
        digits = '0123456789'
        ans = ''
        # ss = s.split(']')
        # print(ss)
        # for si in ss:
        #     if not si:
        #         continue
        #     print(' ', si, end=':')
        #     _si = si.split('[')
        #     print(_si)
        stack = []
        d = 0
        for si in s:
            if si in digits:
                d = d * 10 + int(si)
            elif si == '[':
                stack.append((d, ans))
                ans = ''
                d = 0
            elif si == ']':
                ds = stack.pop()
                ans = ds[1] + ans * ds[0]
            else:
                ans += si
        return ans


solve = Solution()
s0 = "3[a]2[bc]"
print(s0, ':', solve.decodeString(s0))

s1 = "3[a2[bc]]"
print(s1, ':', solve.decodeString(s1))

s2 = "3[abc]3[cd]ef"
print(s2, ':', solve.decodeString(s2))
